<template>
  <div class="page">
    <div class="page-title">{{ titleName }}</div>

    <div class="page-content">
      <search-input title="关键词" :width="250" v-model="searchTxt" placeholder="请输入企业名称或事件关键词"
                    @enter="handlerEnter" @clear="handlerEnter"/>
<!--      <el-button type="primary" size="small" @click="handlerSubmit">查询</el-button>-->
      <div class="page-serach">
        <head-filter ref="filterHead" @changeSelect="changeSelect"></head-filter>
      </div>
      <div class="page-content-screen">
        <div class="screen">
          <!-- <div class="name">更多筛选</div> -->
          <vc-filter :menus="screen"/>
        </div>
      </div>
      <div class="page-content-table">
        <div class="page-content-table-header">
          <market-tab :marketCode="marketCode"/>
          <span class="total">共计 <span style="color: #FF5959;">{{ totalSize}}</span> 条</span>
        </div>
        <vc-table
          :loading="loading"
          :tableData="tableData"
          :columns="columns"
          :total="totalSize"
          :pageSize="PAGE_SIZE"
          :pageNum="queryData.pageNum"
          :defaultSort="{ field: 'issuingDate', order: 'desc' }"
          :maxHeight="maxHeight"
          @page-change="handleCurrentChange"
          @sort-change="sortChange"
        />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import {getEsgList, getEventType} from '../apis';
import {useTable} from './useTable';
import {useScreen} from './useScreen';
import {calculateTableMaxHeight} from "@/utils";

const {setTitle, getTitle} = useTabs();
import searchInput from '@/components/page-search/index.vue';
import MarketTab from '@/views/modules/esg/components/market-tab.vue';
import HeadFilter from "@/views/modules/esg/components/head-filter.vue";
import {useTabs} from "vc";
import {set} from "husky";
const route = useRoute();
const marketCode = route.params.type || 'ag'
const {PAGE_SIZE, screen, queryData, runEsgIndustry} = useScreen(marketCode);
const searchTxt = ref('');
const tableData = ref();
let maxHeight = $ref(0);
let filterHead = ref();
runEsgIndustry({type: marketCode});
const titleName = computed(() => {
  if (marketCode === 'ag') {
    return 'A股争议事件'
  } else {
    return '港股争议事件'
  }
})
setTitle(titleName.value)
const {data, loading, run} = useRequest(getEsgList, {
  defaultParams: [{...queryData, marketCode: marketCode}],
  onSuccess: (data) => {
    tableData.value = data.values;
    nextTick(() => {
      setTimeout(() => {
        maxHeight = calculateTableMaxHeight()
      },300)
    })
  }
});
//页面resize，重新计算高度
window.onresize = function () {
  maxHeight = calculateTableMaxHeight()
}

//选择A股港股
// const changeTab = (type) => {
//   runEsgIndustry({type})
//   queryData.marketCode = type
// }

const {totalSize, columns} = useTable(data);

const isPageChange = ref(false);
const handleCurrentChange = (val: number) => {
  isPageChange.value = true;
  queryData.pageNum = val;
};

const sortChange = (event) => {
  const {column, order} = event;
  const {sortBy} = column;
  queryData.sortRule = sortBy + ':' + `${order ?? 'desc'}`;
};
const handlerEnter = () => {
  queryData.searchTxt = searchTxt.value;
};
//点击查询按钮
const handlerSubmit = () => {
  isPageChange.value = true;
  queryData.pageNum = 1;
  queryData.eventTypeCode = (filterHead.value as any).getValues()
};
//选择事件类型
const changeSelect = () => {
  isPageChange.value = true;
  queryData.pageNum = 1;
  queryData.eventTypeCode = (filterHead.value as any).getValues()
}


watch(
  () => queryData,
  (val) => {
    if (!isPageChange.value) {
      queryData.pageNum = 1;
    } else {
      isPageChange.value = false;
    }
    queryData.searchTxt = searchTxt.value;
    run(val);
  },
  {deep: true}
);
</script>

<style lang="scss" scoped>
@import "./index.scss";

.screen {
  .name {
    display: inline-flex;
    align-items: flex-start;
    font-size: 14px;
  }
}

:deep(.el-button) {
  margin-left: 20px;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner ) {
  background-color: #02C680;
  border-color: #02C680;
}

:deep(.el-checkbox__input.is-checked+.el-checkbox__label) {
  color: #02C680;
}

:deep(.el-checkbox__input.is-indeterminate .el-checkbox__inner) {
  background-color: #02C680;
  border-color: #02C680;
}

:deep(.el-checkbox__input.is-focus .el-checkbox__inner) {
  border-color: #E3E3E3;
}

:deep(.el-checkbox__input .el-checkbox__inner) {
  &:hover{
    border-color: #02C680;
  }
}
</style>
